﻿using B.S.DataBase.MyProject.Domain.基础设置;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.BasicSettings;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.BasicSettings
{
    public class GetTeamInfoByIdCommandHandler : IRequestHandler<GetTeamInfoByIdCommand, APIResult<List<TeamInfo>>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sugar"></param>
        public GetTeamInfoByIdCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<TeamInfo>>> Handle(GetTeamInfoByIdCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<TeamInfo>> result = new APIResult<List<TeamInfo>>();
            try
            {
                var list = sugar.db.Queryable<TeamInfo>().Where(x => !x.IsDel);
                if (!string.IsNullOrEmpty(request.OrgName))
                {
                    list = list.Where(x => x.OrgName.Contains(request.OrgName));
                }
                if (request.TeamID != null)
                {
                    list = list.Where(x => x.TeamID == request.TeamID);
                }
                result.data = list.ToList();
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"添加团队信息时发生异常: {ex.Message}");
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            return await Task.FromResult(result);
        }



    }


}
